package com.future.hedongjun.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.FrameLayout;

import com.future.baselib.activity.BaseActivity;
import com.future.hedongjun.R;
import com.future.hedongjun.home.HomeFragment;
import com.future.hedongjun.mall.MallFragment;
import com.future.hedongjun.mine.MineFragment;
import com.future.hedongjun.mine.bean.UserInfo;
import com.jaeger.library.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.item.NormalItemView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends AppCompatActivity implements OnTabItemSelectedListener {

    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.tab)
    PageNavigationView tab;

    private String[] tags = {"tag1", "tag2", "tag3"};
    public NavigationController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initBottom();
        initFragment();
        JPushInterface.requestPermission(this);
//        initTestUserInfo();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            boolean logout = extras.getBoolean("logout", false);
            if (logout) {
                controller.setSelect(0);
            }
        }

    }

    private void initFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment1 = new HomeFragment();
        transaction.add(R.id.fragment_container, fragment1, tags[0]);
        transaction.commit();
    }

    private void initBottom() {
        controller = tab.custom()
                .addItem(newItem(R.drawable.tab_sy_mr, R.drawable.tab_sy_dq, "首页"))
                .addItem(newItem(R.drawable.tab_sc_mr, R.drawable.tab_sc_dq, "商城"))
                .addItem(newItem(R.drawable.tab_wd_mr, R.drawable.tab_wd_dq, "我的"))
                .build();
        controller.addTabItemSelectedListener(this);
    }

    //创建一个Item
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text) {
        NormalItemView normalItemView = new NormalItemView(this);
        normalItemView.initialize(drawable, checkedDrawable, text);
        normalItemView.setTextDefaultColor(0xff999999);
        normalItemView.setTextCheckedColor(0xffCAD39B);//#CAD39B
        return normalItemView;
    }

    @Override
    public void onSelected(int index, int old) {
        if (index == 2) {
            if (!MyApp.getInstance().checkLogin()) {
                controller.setSelect(old);
                return;
            }
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment1 = fragmentManager.findFragmentByTag(tags[0]);
        Fragment fragment2 = fragmentManager.findFragmentByTag(tags[1]);
        Fragment fragment3 = fragmentManager.findFragmentByTag(tags[2]);

        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }

        switch (index) {
            case 0:
                if (fragment1 == null) {
                    fragment1 = new HomeFragment();
                    transaction.add(R.id.fragment_container, fragment1, tags[0]);
                } else {
                    transaction.show(fragment1);
                }
                break;
            case 1:
                if (fragment2 == null) {
                    fragment2 = new MallFragment();
                    transaction.add(R.id.fragment_container, fragment2, tags[1]);
                } else {
                    transaction.show(fragment2);
                }
                break;
            case 2:
                if (fragment3 == null) {
                    fragment3 = new MineFragment();
                    transaction.add(R.id.fragment_container, fragment3, tags[2]);
                } else {
                    transaction.show(fragment3);
                }
                break;
        }
        transaction.commit();
    }

    @Override
    public void onRepeat(int index) {

    }
}
